"""
@author: tk
@file: shopping_new.py
@time: 2022/11/18 18:49
"""
# 1、登录
# 2、注册
# user = [["sc","123456",100],["sc1","sc123",200]]
# 用户信息： 用户名 密码  余额
user = {"sc": {"passwd": "123456", "money": 100}}
# 商品信息： 价格 名字  编号
products = [["苹果", 5], ["香蕉", 3], ['葡萄', 8], ["榴莲", 40]]
# 购物车： 用户名 商品编号 数量
# car={“sc”:[[0, 2], [1, 3]]}
car = {}


def login(name, passwd):
    with open("message.txt", "r") as fp:
        lists = fp.readlines()
        fp.seek(0)
        name = name + '\n'
        passwd = passwd + '\n'
        if name not in lists:
            print("用户不存在")
            return False
        else:
            name_index = lists.index(name)
            if name in lists and lists[name_index + 1] == passwd:
                return True
                # if name in user and passwd == user[name]["passwd"]:
                #     return True


def show_goods():
    print("编号".ljust(10, " "), "名称".ljust(10, " "), "价格".ljust(10, " "))
    for index, pro in enumerate(products):
        print(str(index).ljust(11, " "), pro[0].ljust(10, " "), str(pro[1]).ljust(10, " "))


def add_car(name, product_id, nums):
    # car={“sc”:[[0, 2], [1, 3]]}
    if name not in car:
        car[name] = []
    car[name].append([product_id, nums])
    print(car)


def checkout(name):
    # user = {"sc": {"passwd": "123456", "money": 0}}
    m1 = user[name]["money"]  # 用户的钱
    car_list = car[name]  # [[0, 2], [1, 3]]
    # car={“sc”:[[0, 2], [1, 3]]}
    # products = [["苹果", 5], ["香蕉", 3], ['葡萄', 8], ["榴莲", 40]]
    total = 0
    print("编号  ", "名称  ", "单价", "数量", "总价")
    for pro in car_list:
        # pro -->[1, 3] #[商品编号， 商品数量]
        pro_name = products[pro[0]][0]
        price = products[pro[0]][1]
        pro_total = price * pro[1]
        total += pro_total
        print(pro[0], pro_name, price, pro[1], pro_total)
    print("所有商品总价为：", total)
    if m1 >= total:
        m1 -= total
    else:
        print("余额不足！")
    # 修改用户金额
    user[name]["money"] = m1
    print(user)


def menu():
    print("*" * 20)
    print("1、查看商品列表")
    print("2、添加到购物车")
    print("3、结算")
    print("4、退出")


def regist(name_re, passwd_re):
    user[name_re] = {"passwd": passwd_re, "money": 0}
    print("注册成功!")
    with open("message.txt", "a") as fp:
        fp.write(name_re + '\n' + passwd_re + '\n' + "0" + '\n')


print("欢迎来到三乐购物系统".center(50, "*"))
# print("1.登录")
# print("2.注册")
# options = input("请输入你的选择：")
while 1:
    print("1.登录")
    print("2.注册")
    options = input("请输入你的选择(按q退出):")
    if options == "1":
        name = input("请输入用户名:")
        passwd = input("请输入密码:")
        if login(name, passwd):
            while 1:
                menu()
                op = input("请输入你的操作:")
                if op == "1":
                    show_goods()
                elif op == "2":
                    product_id = input("请输入添加到购物车的商品编号：")
                    if product_id.isdigit() and 0 <= int(product_id) < len(products):
                        nums = int(input("请输入需要购买的数量:"))
                        add_car(name, int(product_id), nums)
                elif op == "3":
                    checkout(name)
                elif op == "4":
                    break
        else:
            print("登录失败！ 用户名或密码错误！")
    elif options == "2":
        print("欢迎来到注册界面".center(50, "*"))
        name_re = input("请输入注册用户名:")
        if name_re in user:
            print("用户名已存在,请重新输入")
            continue
        passwd_re = input("请输入注册密码:")
        regist(name_re, passwd_re)



    elif options == "q":
        break
    else:
        print("输入不合法！")
